package com.kexin.coupon.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kexin.common.exception.WebException;
import com.kexin.common.user.entity.UserBase;
import com.kexin.common.user.service.IUserBaseService;
import com.kexin.common.utils.PageData;
import com.kexin.common.utils.Result;
import com.kexin.coupon.entity.Coupon;
import com.kexin.coupon.service.ICouponOrderService;
import com.kexin.coupon.service.ICouponService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 卡券信息表
 *
 * @author ao
 * @since 2021-07-20
 */
@Api(tags = "卡券")
@RestController
@RequestMapping("/coupon")
public class CouponController {

	Logger log = LoggerFactory.getLogger(CouponController.class);

	@Autowired
	private ICouponService couponService;

	@Autowired
	private ICouponOrderService couponOrderService;

	@Autowired
	private IUserBaseService userBaseService;

	@ApiOperation("获取卡券列表")
    @ApiImplicitParams({
		@ApiImplicitParam(name="couponName",value="卡券名称（模糊查询）",dataType="String",paramType="query"),
		@ApiImplicitParam(name="current",value="页码",dataType="int",paramType="query"),
		@ApiImplicitParam(name="size",value="单页显示条数",dataType="int",paramType="query")
	})
    @PostMapping("/list")
	public Result<PageData<Coupon>> list(String couponName, 
			Integer current, Integer size){
		try {
			return new Result<PageData<Coupon>>(new PageData<Coupon>(
					couponService.queryList(couponName, 1, current, size)));
		} catch (Exception e) {
			e.printStackTrace();
            throw new WebException("获取卡券列表出错", e);
		}
	}

	@ApiOperation("获取单个卡券详情")
    @ApiImplicitParams({
		@ApiImplicitParam(name="id",value="卡券id",dataType="String",paramType="query")
	})
    @PostMapping("/one")
	public Result<Coupon> one(Integer id){
		try {
			if (id==null) {
				return new Result<Coupon>(false, 1, "参数id为空");
			}
			Coupon coupon = couponService.getOne(id);
			if (coupon==null) {
				return new Result<Coupon>(false, 1, "此卡券信息不存在");
			}
			
			try {
				//登录用户查询购买了的数量
				UserBase userBase = userBaseService.getCurrentUser();
				if (userBase!=null) {
					Integer userId = userBase.getId();
					if (userId!=null) {
						int userBuyedCount = couponOrderService.getUserBuyedCount(
								userId, coupon.getCouponCode(), "0", "1");
						Integer buyMax = coupon.getBuyMax();
						if (buyMax==null) {
							buyMax = -1;
						}
						if (buyMax>=0 && userBuyedCount>=buyMax) {
							coupon.setBuyFull(true);
						}
					} else {
						log.info("未登录不获取已经购买的数量");
					}
				} else {
					log.info("未登录不获取已经购买的数量");
				}
			} catch (Exception e) {
				e.printStackTrace();
				log.info("未登录不获取已经购买的数量");
			}
			
			return new Result<Coupon>(coupon);
		} catch (Exception e) {
			e.printStackTrace();
            throw new WebException("获取单个卡券详情出错", e);
		}
	}
}
